package comment

import (
	"fmt"
	"html"
	"strings"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Rt"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/App/meta"
)

func validateCommentData(data map[string]string) string {
	if Rt.Validator.IsEmptyString(data["author"]) {
		return "称呼必填"
	}
	if !Rt.Validator.IsEmail(data["email"]) {
		return "邮箱格式错误"
	}
	if data["url"] != "" {
		if !Rt.Validator.IsURL(data["url"]) {
			return "网址格式错误"
		}
	}
	if Rt.Validator.IsEmptyString(data["content"]) {
		return "内容没填"
	}
	return ""
}

func AddComment(belong string, data map[string]string) map[string]string {
	data["content"] = html.EscapeString(data["content"])
	data["content"] = strings.Replace(data["content"], "\n", "<br/>", -1)
	if data["url"] == "" {
		data["url"] = "#"
	}
	if data["parent"] != "0" {
		c := GetOne(data["parent"]);
		if len(c) > 0 {
			data["content"] = `<a class="comment-parent-name" href="#comment-` + c["id"] + `">@` + c["author"] + `</a>` + data["content"]
		}
	}
	sql := Core.Sql("fuxiaohei_comments", "author,email,url,content,add_time,belong_id,status,parent_id").Insert()
	id := Core.Db.Exec(sql, data["author"], data["email"], data["url"], data["content"], Rt.Time.Now(), "article-" + belong, getEmailStatus(data["email"]), data["parent"])
	go UpdateCount(belong)
	return GetOne(fmt.Sprint(id))
}

func getEmailStatus(email string) string {
	sql := Core.Sql("fuxiaohei_comments").Where("email = ?").Where("status = ?").Count()
	data := Core.Db.One(sql, email, "spam")
	if len(data) > 0 {
		if data["countNum"] != "0" {
			return "spam"
		}
	}
	data = Core.Db.One(sql, email, "pending")
	if len(data) > 0 {
		if data["countNum"] != "0" {
			return "pending"
		}
	}
	data = Core.Db.One(sql, email, "approved")
	if len(data) > 0 {
		if data["countNum"] == "0" {
			return "pending"
		}
	}
	return "approved"
}


func GetList(belong string) []map[string]string {
	sql := Core.Sql("fuxiaohei_comments").Where("belong_id = ?").Order("id ASC")
	commentDisplay := meta.MetaString("comment_display")
	if commentDisplay == "hide" {
		sql.Where(`status = "approved"`)
	}else {
		sql.Where(`status != "spam"`)
	}
	comments := Core.Db.All(sql.Select(), "article-" + belong)
	for k, v := range comments {
		comments[k]["avatar"] = Rt.Media.Gravatar(v["email"], "40")
		delete(comments[k], "email")
		delete(comments[k], "belong_id")
	}
	return comments
}

func UpdateCount(articleId string) {
	sql := `UPDATE fuxiaohei_articles SET comments = (SELECT count( * ) FROM fuxiaohei_comments AS c WHERE c.[belong_id] = ? AND c.parent_id < 1 AND c.user_id == 0 AND c.status == "approved") WHERE fuxiaohei_articles.id = ?`
	Core.Db.Exec(sql, "article-" + articleId, articleId)
}

func RecentComments(size int) []map[string]string {
	sql := Core.Sql("fuxiaohei_comments", "id,author,belong_id,content").Where("status = ?").Where("parent_id = ?")
	sql.Order("add_time DESC").Limit(size)
	comments := Core.Db.All(sql.Select(), "approved", 0)
	if len(comments) > 0{
		tmpArticles := map[string]map[string]string{}
		for k,v := range comments{
			if tmpArticles[v["belong_id"]] == nil {
				tmpArticles[v["belong_id"]] = articleGetById(strings.Replace(v["belong_id"], "article-", "", -1))
			}
			comments[k]["article_title"] = tmpArticles[v["belong_id"]]["title"]
			comments[k]["article_slug"] = tmpArticles[v["belong_id"]]["slug"]
		}
	}
	return comments
}




